package com.isecta.irobot.service;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.browserlaunchers.locators.GoogleChromeLocator;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class Worker1 {

	public static void main(String s[]) {
		// FirefoxDriver driver = new FirefoxDriver();
		InternetExplorerDriver driver = new InternetExplorerDriver();
		// driver.get("https://accounts.craigslist.org/login");
		driver.get("http://bangladesh.craigslist.org/cas/");
		List<WebElement> elements = driver.findElementsByTagName("a");
		for (WebElement elementLink : elements) {
			if (elementLink.getAttribute("href") != null
					&& elementLink.getAttribute("href").contains(
							"https://post.craigslist.org/")) {
				elementLink.click();
				break;
			}
		}
		elements = driver.findElementsByTagName("input");
		for (WebElement elementLink : elements) {
			if (elementLink.getAttribute("value") != null
					&& elementLink.getAttribute("value")
							.equalsIgnoreCase("cas")) {
				elementLink.click();
				break;
			}
		}

		WebElement element = driver.findElementById("w");
		if (element == null)
			return;
		element.click();

		element = driver.findElementById("m2");
		if (element == null)
			return;
		element.click();

		elements = driver.findElementsByTagName("button");
		for (WebElement elementLink : elements) {
			if (elementLink.getAttribute("value") != null
					&& elementLink.getAttribute("value").equalsIgnoreCase(
							"Continue")) {
				elementLink.submit();
				break;
			}
		}

		elements = driver.findElementsByTagName("div");
		String location = "";
		for (WebElement elementLink : elements) {
			if (elementLink.getAttribute("class") != null
					&& elementLink.getAttribute("class").equalsIgnoreCase(
							"contents")) {
				location = elementLink.getText();
				break;
			}
		}

		String loc = "";
		if (!location.isEmpty()) {
			String[] locations = location.split(">");
			if (locations.length == 5) {
				loc = locations[2];
			} else {
				loc = locations[1];
			}
			System.out.println(location + "::" + loc);
		}

		int count = 1;
		String postTitle = "Title";
		int postAge = 12;
		String postLoc = loc;
		String postDesc = "description";

		elements = driver.findElementsByTagName("input");
		for (WebElement elementLink : elements) {
			if (elementLink.getAttribute("type") != null
					&& elementLink.getAttribute("type")
							.equalsIgnoreCase("text")) {
				if (count == 1) {
					elementLink.sendKeys(postTitle);
				} else if (count == 2) {
					elementLink.sendKeys("" + postAge);
				} else if (count == 3) {
					elementLink.sendKeys("" + postLoc);
				} else if (count == 6) {
					elementLink.sendKeys("" + postDesc);
				}
				count++;
			}
		}

		count = 1;
		elements = driver.findElementsByTagName("textarea");
		for (WebElement elementLink : elements) {
			if (count == 1) {
				elementLink.sendKeys("" + postDesc);
				break;
			}
		}
		
		/* Final submit */
		/*elements = driver.findElementsByTagName("button");
		for (WebElement elementLink : elements) {
			if (elementLink.getAttribute("value") != null
					&& elementLink.getAttribute("value").equalsIgnoreCase(
							"Continue")) {
				elementLink.submit();
				break;
			}
		}*/
		System.out.println("click found:::");
		// WebElement eleEmail = driver.findElement(By.id("inputEmailHandle"));
		// WebElement elePass = driver.findElement(By.id("inputPassword"));
		// eleEmail.sendKeys("test@yahoo.com");
		// elePass.sendKeys("password");
	}
}
